package top.glkj.hms.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.Data;

import java.io.Serializable;
import java.lang.reflect.Type;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/10 17:36
 */
@Data
public final class Result implements Serializable {
    static final long serialVersionUID = 20000213L;
    private int code;
    private String message;
    private Object object;

    public Result() {
        new Result(code);
    }

    public Result(int code) {
        new Result(code, "");
    }

    public Result(int code, String message) {
        new Result(code, message, null);
    }

    public Result(int code, String message, Object object) {
        this.code = code;
        this.message = message;
        this.object = object;
    }

    public static String objectToJsonString(Object object) {
        Gson gson = new GsonBuilder().create();
        Type type = new TypeToken<Object>() {
        }.getType();
        //return JSONObject.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);
        return gson.toJson(object, type);
    }
}
